/*8．巨人航空公司的机群由12个座位的飞机组成。它每天飞行一个航班。根据下面的要求，编写一个座
位预订程序。
a．该程序使用一个内含12个结构的数组。每个结构中包括：一个成员表示座位编号、一个成员表
示座位是否已被预订、一个成员表示预订人的名、一个成员表示预订人的姓。

b．该程序显示下面的菜单：
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
q) Quit

c．该程序能成功执行上面给出的菜单。选择d)和e)要提示用户进行额外输入，每个选项都能让用
户中止输入。
d．执行特定程序后，该程序再次显示菜单，除非用户选择q)。
*/

#include <stdio.h>
#include <string.h>

#define NUM 12
#define LENGTH 15

typedef struct{
    char seatnum[LENGTH];
    int status;
    char lname[LENGTH];
    char fname[LENGTH];
}Struct;

void menu();
void showEmptyNum(Struct info[], int num);
void showEmptyList(Struct info[], int num);
void showEmptyQueue();
void assign(Struct info[], int num);
void deleteAssign(Struct info[], int num);
void menu();

int main(){
    Struct info[NUM] = {
        {"A", 0, "", ""},{"B", 0, "", ""},{"C", 0, "", ""},{"D", 0, "", ""},
        {"E", 0, "", ""},{"F", 0, "", ""},{"G", 0, "", ""},{"H", 0, "", ""},
        {"I", 0, "", ""},{"J", 0, "", ""},{"K", 0, "", ""},{"L", 0, "", ""},
    };
    char ch;
    menu();
    while((ch = getchar()) != 'q'){
        while(getchar() != '\n')continue;
        switch(ch)
        {
            case 'a':showEmptyNum(info, NUM);break;
            case 'b':showEmptyList(info, NUM);break;
            case 'c':showEmptyQueue();break;
            case 'd':assign(info, NUM);break;
            case 'e':deleteAssign(info, NUM);break;
            default:printf("Please confirm your input\n");break;
        }
        menu();
    }
    printf("Goodbye!\n");
    return 0;
}

/*
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
q) Quit
*/

void menu(){
    printf("*********************************************\n");
    printf("*********************************************\n");
    printf("To choose a function, enter its letter label:\n");
    printf("a) Show number of empty seats\n");
    printf("b) Show list of empty seats\n");
    printf("c) Show alphabetical list of seats\n");
    printf("d) Assign a customer to a seat assignment\n");
    printf("e) Delete a seat assignment\n");
    printf("q) Quit\n");
    printf("*********************************************\n");
    printf("*********************************************\n");
}

void showEmptyNum(Struct info[], int num){
    int count = 0;
    for(int i = 0; i < num; i++){
        if(info[i].status == 0)
        {
            count++;
        }
    }
    printf("The number of empty seats is %d\n", count);
}

void showEmptyList(Struct info[], int num){
    for(int i = 0; i < num; i++){
        if(info[i].status == 0)
        {
            printf("%s ", info[i].seatnum);
        }
    }
    printf("\n");
}

void showEmptyQueue(){
    printf("还未实现\n");
}

void assign(Struct info[], int num){
    for(int i = 0; i < num; i++){
        if(info[i].status == 0)
        {
            printf("Please enter your firstname\n");
            scanf("%s", info[i].fname);
            printf("Please enter your lastname\n");
            scanf("%s", info[i].lname);
            info[i].status = 1;
            printf("Your sear has been assigned successfully!"
            "The seat number is %s\n", info[i].seatnum);
            break;
        }
    }
}

void deleteAssign(Struct info[], int num){
    char tempname[LENGTH];
    printf("Please enter your firstname\n");
    scanf("%s", tempname);
    for(int i = 0; i < num; i++){
        if((strcmp(info[i].fname, tempname)) == 0)
        {
            info[i].status = 0;
            strcpy(info[i].fname, "");
            strcpy(info[i].lname, "");
            printf("Your sear has been deleted successfully!"
            "The seat number is %s\n", info[i].seatnum);
            break;
        }
    }
}    